package com.yhp.dao.impl;

import com.yhp.bean.Users;
import com.yhp.dao.UsersDao;
import com.yhp.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UsersDaoImpl extends DBUtils implements UsersDao {
    @Override
    public Users login(String username, String password) {
        ResultSet resultSet = null;
        Users users = null;
        try {
            String sql = "select * from users where loginname = ? and password = ?";
            ArrayList arrayList = new ArrayList();
            arrayList.add(username);
            arrayList.add(password);
            resultSet = query(sql, arrayList);
            if(resultSet==null){
                return null;
            }
            while(resultSet.next()){
                users=new Users();
                users.setLoginName(username);
                users.setRealname(resultSet.getString("realname"));
                users.setUserId(resultSet.getInt("userid"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return users;
    }
}
